/**
 * Qiangungun.sms Inc.
 * Copyright (c) 2015-2015 All Rights Reserved.
 */
package com.zbest.zpay.paycore.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zbest.common.lang.enums.BizCode;
import com.zbest.common.lang.exception.ZpayException;
import com.zbest.zpay.paycore.helper.ProcessFlowManager;
import com.zbest.zpay.service.paycore.PayService;
import com.zbest.zpay.service.paycore.model.PayInfoDTO;
import com.zbest.zpay.service.paycore.model.PayType;
import com.zbest.zpay.service.paycore.model.PaycoreKeyEnum;
import com.zbest.zpay.service.paycore.model.PaycoreResult;
import com.zbest.zpay.shared.process.ProcessResult;
import com.zbest.zpay.shared.process.ProcessService;

/**
 * 支付服务接口实现类
 * 
 * @author hangdahong
 * @version $Id: PayNotifyServiceImpl.java, v 0.1 2015年1月20日 下午10:27:23
 *          hangdahong Exp $
 */
@Service("payService")
public class PayServiceImpl implements PayService {

    private static final Logger logger = LoggerFactory.getLogger(PayServiceImpl.class);

    @Resource
    private ProcessService      processService;

    @Resource
    private ProcessFlowManager  processFlowManager;

    /**
     * @see com.zbest.zpay.service.paycore.PayService#pay(com.zbest.zpay.service.paycore.model.PayInfoDTO)
     */
    @Override
    public PaycoreResult pay(PayInfoDTO request) {

        request.setPayType(PayType.DEPOSIT);
        return processRequest(request);
    }

    /**
     * @see com.zbest.zpay.service.paycore.PayService#refund(com.zbest.zpay.service.paycore.model.PayInfoDTO)
     */
    @Override
    public PaycoreResult refund(PayInfoDTO request) {
        request.setPayType(PayType.REFUND);
        return processRequest(request);
    }

    /**
     * @see com.zbest.zpay.service.paycore.PayService#confirmPay(com.zbest.zpay.service.paycore.model.PayInfoDTO)
     */
    @Override
    public PaycoreResult confirmPay(PayInfoDTO request) {

        request.setPayType(PayType.CONFIRM_DEPOSIT);
        return processRequest(request);
    }

    /**
     * @see com.zbest.zpay.service.paycore.PayService#processRequest(com.zbest.zpay.service.paycore.model.PayInfoDTO)
     */
    private PaycoreResult processRequest(PayInfoDTO request) {

        logger.info("接收支付请求：" + request);
        PaycoreResult paymentResult = new PaycoreResult();
        try {
            String flow = processFlowManager.route(request);
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(PaycoreKeyEnum.PAY_INFO.getCode(), request);
            ProcessResult result = processService.execute(flow, dataMap);
            paymentResult.setCode(result.getCode());
            paymentResult.getResultData().putAll(result.getResultObject());
        } catch (ZpayException e) {
            logger.error("支付处理异常request=" + request, e);
            paymentResult.setCode(e.getCode());
        } catch (Exception e) {
            logger.error("支付处理异常request=" + request, e);
            paymentResult.setCode(BizCode.SYSTEM_EXCEPTION);
        }
        logger.info("支付响应：" + paymentResult);
        return paymentResult;
    }

}
